package Server.Dwc.shz;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Vector;
import net.sf.json.*;
import redis.protocal.DbInfoMgr;
import Server.Agent.GameAgent;
import Server.Agent.GameDB;
import Server.Model.Base.IRoom;
import Server.Model.Base.IServerUserItem;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import club.ClubRedis;
import common.routine.PublishDef;
import Server.Model.Base.SecShmUser;
import Server.Model.Base.UserGoldChange;
import Server.Model.Base.UserRmbChange;

public class shzTable extends IRoom {
	@Override
	public int GetID() {
		return m_dwTableID;
	};

	@Override
	public int GetCreator() {
		return m_Creator;
	}
	@Override
	public String GetType() {
		return "SHZ";
	};
	@Override
	public void OnClientClose(int uid) {
		OnUserStandUp(uid,false);
	};

	public boolean SendData(int wChairID, String sData) {
		if (wChairID == Constants.INVALID_CHAIR) {
			for (short i = 0; i < m_pIUserItem.length; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					if (m_pIUserItem[i].isAndroidUser == 0) {
						dthall.getInstance().onSend(m_pIUserItem[i].dwUserID, sData);
					} else {
						// mPlayers[i].SendGameMessage(wSubCmdID, pData,
						// wDataSize, data);
					}
				}
			}

			return true;
		} else {
			if (wChairID >= m_pIUserItem.length) {
				dthall.getInstance().onSend(wChairID, sData);
			} else if (m_pIUserItem[wChairID].dwUserID > 0) {
				if (m_pIUserItem[wChairID].isAndroidUser == 0) {
					dthall.getInstance().onSend(m_pIUserItem[wChairID].dwUserID, sData);
				} else {
				}
				return true;
			}
		}

		return false;
	}

	int m_dwLastDoCmdTime;

	public int CanSit(int _Uid) {
		
		if (m_locked != 0) {
			return 13;
		}

		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].dwUserID == _Uid) {
				SUBCMD_S_OnLine cmd = new SUBCMD_S_OnLine();
				cmd.uid = _Uid;
				cmd.seat = i;
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
				return i;
			}
		}

		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null && isZuJuType()) {
			if (m_payWay.equalsIgnoreCase("CLUBPAY") || m_payWay.equalsIgnoreCase("WINPAY")) {
				if(m_payWay.equalsIgnoreCase("CLUBPAY")){
					for (int i = 0; i < m_maxUserNum; i++) {
						if (m_pIUserItem[i].dwUserID == 0) {
							return i;
						}
					}
					return 12;
				}
				if (user.getGem(0) < m_byCostGem * m_maxUserNum) {
					return 11;
				}
			}else{
				if (m_payWay.equalsIgnoreCase("AAPAY") )
				{
					if (user.getGem(0) < m_byCostGem) {
						return 11;
					}
				}
			}
		}

		if(user != null && isGoldType()){
			if(user.getCoin(0) < m_ruchang){
				return 21;
			}
		}
		if(user != null && isDwcType()){
			if(user.getRmb1v1Value() < m_ruchang){
				return 21;
			}
		}
	
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				return i;
			}
		}
		return 12;
	};

	Long m_lStartTime;
	int needGameStats;
	long needDealTime;
	int m_dwTableID;
	long m_dismissTimer;


	boolean m_bGameStarted;
	boolean m_bTableStarted;
	int m_byRound;
	int m_byMaxRound;
	int m_wFaPaiDelay;
	
	int m_nextUser = 0;

	public int m_byCostGem;
	public String m_payWay;
	public String m_strParam;
	public String m_strRoomDesc;
	public String[] m_vecParam;

	GameLogic m_GameLogic = new GameLogic();
	String m_curStage;
	
	int m_locked = 0;
	public int m_maxUserNum = 1;
	public int m_vecWinScore[] = new int[m_maxUserNum];
	public int m_vecLoseScore[] = new int[m_maxUserNum];
	public int m_vecCardType[] = new int[m_maxUserNum];
	public int m_vecRoundScore[] = new int[m_maxUserNum];
	public int m_vecResultScore[] = new int[m_maxUserNum];
	public double m_doubleVecResultScore[] = new double[m_maxUserNum];
	IServerUserItem m_pIUserItem[] = new IServerUserItem[m_maxUserNum];
	{
		for (int i = 0; i < m_maxUserNum; i++) {
			m_pIUserItem[i] = new IServerUserItem();
		}
	}
	Map<Integer, Integer> m_mapStartIndex = new HashMap<Integer, Integer>();
	public int rdRate;
	public int lastRateTime;
	int m_byAgreeDismiss[] = new int[m_maxUserNum];

	public int m_Creator;
	
	int[] m_UserStatus = new int[m_maxUserNum];
	int UserCntReady = 0;
	long startDealTime = 30;

	shzTable() {
		m_bTableStarted = false;
		m_lStartTime = System.currentTimeMillis() / 1000;
		m_bRemove = false;
		m_byCostGem = 0;
		m_payWay = "FZPAY";
		
		m_strParam = "";
		m_strRoomDesc = "";
		setRoomStarted(true);
	}

	void Init(int cbReason) {
		rdRate = 0;
		lastRateTime = 0;
		m_dwLastDoCmdTime = 0;
		m_bGameStarted = false;
		m_wFaPaiDelay = 0;
		UserCntReady = 0;
		startDealTime = 30;
		for (int i = 0; i < m_maxUserNum; i++) {
			m_vecCardType[i] = 0;

			m_vecRoundScore[i] = 0;
			m_UserStatus[i] = 0;
		}
		switch (cbReason) {
		case Constants.INIT_TOTAL: {
			m_dismissTimer = 0;
			for(int i = 0;i < m_maxUserNum;i++){
				m_byAgreeDismiss[i] = 0;
			}
			m_byRound = 0;
			m_byMaxRound = 0;
			for (int i = 0; i < m_maxUserNum; i++) {
				m_vecWinScore[i] = 0;
				m_vecLoseScore[i] = 0;
				m_vecResultScore[i] = 0;
				m_doubleVecResultScore[i] = 0;
			}
		}
			break;
		case Constants.INIT_FIRST_ROUND: {

		}
			break;
		case Constants.INIT_NEWROUND: {

		}
			break;
		}
	}

	void KickRobots() {
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
	}

	boolean HasRobot() {
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				return true;
			}
		}
		return false;
	}

	void SendAvatar(int _Uid) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}

		String iter = dthall.getInstance().Handle_OnGetUserAvatar(_Uid);

		SUBCMD_S_Avatar cmd = new SUBCMD_S_Avatar();
		cmd.m_lUid = _Uid;
		if (iter != null) {
			cmd.szAvatarURL = iter;
		} else {
			cmd.szAvatarURL = PublishDef.getDefaultHeadAvatar();
		}

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}
	public void SendLocationData(SHMUser user) {
		JSONObject data = new JSONObject();
		data.put("loc", user.getGpsLocInfo());
		data.put("uid", user.getUid());
		data.put("command", "GPS_LOC");
		SendData(Constants.INVALID_CHAIR, data.toString());

		for (short i = 0; i < m_pIUserItem.length; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_pIUserItem[i].isAndroidUser == 0) {
					int uid = m_pIUserItem[i].dwUserID;
					SHMUser other = dthall.getInstance().getUserInfo(uid);
					if(other != null){
						data.put("uid", other.getUid());
						data.put("loc", other.getGpsLocInfo());
						dthall.getInstance().onSend(user.getUid(), data.toString());
					}
				}
			}
		}
	}

	public void SendGpsData(SHMUser user) {
		JSONObject data = new JSONObject();
		List<Double> gpsData = user.getLocateInfo();
		data.put("lng", gpsData.get(0));
		data.put("lat", gpsData.get(1));
		data.put("command", "GPS_DATA");
		data.put("uid", user.getUid());
		SendData(Constants.INVALID_CHAIR, data.toString());

		for (short i = 0; i < m_pIUserItem.length; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_pIUserItem[i].isAndroidUser == 0) {
					int uid = m_pIUserItem[i].dwUserID;
					SHMUser other = dthall.getInstance().getUserInfo(uid);
					if (other != null) {
						gpsData = other.getLocateInfo();
						data.put("uid", other.getUid());
						data.put("lng", gpsData.get(0));
						data.put("lat", gpsData.get(1));
						dthall.getInstance().onSend(user.getUid(), data.toString());
					}
				}
			}
		}
	}
	public void OnUserSit(int _Uid) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < m_maxUserNum) {
			m_pIUserItem[bySeatID].cbOnline = Constants.US_NULL;
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);

		if (user != null) {
			for (int i = 0; i < m_maxUserNum; i++) {
				if (m_pIUserItem[i].dwUserID == _Uid) {
					m_pIUserItem[i].dwUserID = _Uid;
					// m_pIUserItem[i].lUserScore = user.getCoin();
					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);
					m_pIUserItem[i].lUserCoin = user.getCoin(0);
					if(this.isDwcType()){
						m_pIUserItem[i].lUserRmb = user.getRmb1v1Value();
					}

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = user.getIP(null);
					//cmd.m_byRound = m_UserStatus;
					if(this.isGoldType())
						cmd.m_lUserCoin = user.getCoin(0);
					else if(this.isDwcType())
						cmd.m_lUserCoin = user.getRmb1v1Value();
					
					SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					SendAvatar(m_pIUserItem[i].dwUserID);

					return;
				}
			}
			for (int i = 0; i < m_maxUserNum; i++) {
				if (m_pIUserItem[i].dwUserID == 0) {
					m_pIUserItem[i].dwUserID = _Uid;
					m_pIUserItem[i].lUserScore = 0;
					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);
					m_pIUserItem[i].lUserCoin = user.getCoin(0);
					if(this.isDwcType()){
						m_pIUserItem[i].lUserRmb = user.getRmb1v1Value();
					}

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = user.getIP(null);
					//cmd.m_byRound = m_UserStatus;
					if(this.isGoldType())
						cmd.m_lUserCoin = user.getCoin(0);
					else if(this.isDwcType())
						cmd.m_lUserCoin = user.getRmb1v1Value();
					
					SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					return;
				}
			}
		}
		return;
	}

	int FindSeat(long _Uid) {
		for (int bySeatID = 0; bySeatID < m_maxUserNum; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}
	int random(int iMin, int iMax) {
		Random ra = new Random();
		return ra.nextInt(iMax - iMin) + iMin;
	}

	public void memcpy(int[] o, int[] b, int l) {
		for (int i = 0; i < l; i++) {
			o[i] = b[i];
		}
	}

	public void Handle_onGuess(GameAgent agent, String[] vecArg) {
		//中奖后猜大小
		if (vecArg.length < 2)
			return;
		String str1 = vecArg[1];
		String str2 = vecArg[2];

		int nGuessBigSmall = Integer.parseInt(str1);
		double nBet = Double.parseDouble(str2);
		BigDecimal b = new BigDecimal(nBet);
		nBet = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 
		double myCredit = 0;
		if(this.isDwcType()){
			myCredit = agent.getUser().getRmb1v1Value();
			if (nBet > myCredit) {
				return;
			}
		}else{
			myCredit = agent.getUser().getCoin(0);
			if (nBet > myCredit) {
				return;
			}
		}

		SUBCMD_S_Guess cmd = new SUBCMD_S_Guess();

		if (random(0, 10000) > 7200) {// 赢了
			cmd.byGuessBigSmall = nGuessBigSmall; // 压的是大还是小
			cmd.byGuessNumber = nGuessBigSmall > 0 ? random(8, 13) : random(1, 7);// >0压得大
			cmd.winCoin = nBet * 2;
			cmd.totalCoin = myCredit + nBet;
			if(this.isDwcType()){
				agent.getUser().addRmb1v1Value(nBet);
				SecShmUser xianUser = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[0].dwUserID);
				UserRmbChange.handleRmbChangeWinLose(xianUser,nBet,UserRmbChange.reason31);
			}else{
				agent.getUser().getCoin((int)nBet);
				SecShmUser xianUser = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[0].dwUserID);
				UserGoldChange.handleGoldChangeWinLose(xianUser,(int)(nBet),UserGoldChange.reason31);
			}
		} else {// 输了
			cmd.byGuessBigSmall = nGuessBigSmall; // 压的是大还是小
			cmd.byGuessNumber = nGuessBigSmall <= 0 ? random(8, 13) : random(1, 7);// >0压得大
			cmd.winCoin = 0;
			cmd.totalCoin = myCredit - nBet;
			if(this.isDwcType()){
				agent.getUser().addRmb1v1Value(0 - nBet);
				SecShmUser xianUser = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[0].dwUserID);
				UserRmbChange.handleRmbChangeWinLose(xianUser,-nBet,UserRmbChange.reason31);
			}else{
				agent.getUser().getCoin((int)(0 - nBet));
				SecShmUser xianUser = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[0].dwUserID);
				UserGoldChange.handleGoldChangeWinLose(xianUser,(int)(-nBet),UserGoldChange.reason31);
			}
		}

		agent.SendMessage(cmd.getJsonData());
	}

	int GetPlayerCount() {
		int cntPlayer = 0;
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	int GetRobotCount() {
		int cntPlayer = 0;
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	public void OnUserStandUp(int _Uid,boolean leaveroom) {
		int wChairID = FindSeat(_Uid);
		
		if(m_bTableStarted == false && wChairID != 0 && leaveroom == true){
			SUBCMD_S_StandUp cmd = new SUBCMD_S_StandUp();
			cmd.m_lUid = _Uid;
			cmd.m_wUserChairID = wChairID;
			cmd.m_dwTableID = m_dwTableID;
			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
		}


		if (m_bTableStarted == false && leaveroom == true) {
			if (wChairID > 0 && wChairID < m_maxUserNum) {
				m_pIUserItem[wChairID].Init();
				QpGameMgr.getInstance().OnRemoveUserRoom(_Uid);
			} else {
			}
		} else {
			if (wChairID < m_maxUserNum) {
				m_pIUserItem[wChairID].cbOnline = Constants.US_OFFLINE;
			}
		}	
	}
	public void SetParam(String szParam) {
		m_strParam = szParam;
		
		m_vecParam = m_strParam.split(";");
	}

	public void SetDesc(String szDesc) {
		m_strRoomDesc = szDesc;

	}

	boolean SendGameScene(int _Uid, int bGameStatus) {
		int wChairID = FindSeat(_Uid);
		if(wChairID >= m_maxUserNum)
		{
			return false;
		}
		if (m_strParam.length() > 0) {
			String tmpRoomDesc = m_strRoomDesc;
			
			SUBCMD_S_Param cmd = new SUBCMD_S_Param();
			cmd.m_dwTableID = m_dwTableID;
			cmd.szParam = m_strParam;
			cmd.szRoomDesc = tmpRoomDesc;
			SendData(_Uid, cmd.getJsonData());
		}
		
		int cntUser = 0;
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		{
			SUBCMD_S_Table cmd = new SUBCMD_S_Table();
			cmd.m_dwTableID = m_dwTableID;// ����ID
			cmd.m_bGameStarted = m_bGameStarted ? 1 : 0;// �Ƿ�ʼ
			cmd.m_bTableStarted = m_bTableStarted ? 1 : 0;// �Ƿ�ʼ
			cmd.m_byRound = m_byRound;
			cmd.m_byMaxRound = m_byMaxRound;
			cmd.m_cntUser = cntUser;// ���
			cmd.m_cntLookon = 1;// ����
			Constants.memset(cmd.m_seatInfo, 0, Constants.sizeof(cmd.m_seatInfo));

			for (int i = 0; i < m_maxUserNum; i++) {
				cmd.m_seatInfo[i].m_lUid = m_pIUserItem[i].dwUserID;// UID
				cmd.m_seatInfo[i].m_iTotalCoin = m_pIUserItem[i].lUserScore;// COIN
				cmd.m_seatInfo[i].m_NickName = m_pIUserItem[i].szNickName;// NICKNAME
				cmd.m_seatInfo[i].m_iBetCoin = 0;//
				cmd.m_seatInfo[i].m_cbGender = m_pIUserItem[i].cbGender;
				cmd.m_seatInfo[i].m_iAvatar = m_pIUserItem[i].dwFaceID;//
				cmd.m_seatInfo[i].m_cbUserStatus = m_pIUserItem[i].cbUserStatus;// �û�״̬
				cmd.m_seatInfo[i].m_iVipLevel = m_pIUserItem[i].cbVipLevel;// VIP
																			// LEVEL
				cmd.m_seatInfo[i].m_iTitle = 0;// �ƺ�
				cmd.m_seatInfo[i].m_isLookon = 0;// �Ƿ����
				cmd.m_seatInfo[i].szIP = m_pIUserItem[i].szIP;// NICKNAME
				if(this.isGoldType())
					cmd.m_seatInfo[i].m_iTotalCoin = m_pIUserItem[i].lUserCoin;
				else if(this.isDwcType())
					cmd.m_seatInfo[i].m_iTotalCoin = m_pIUserItem[i].lUserRmb;
			}

			SendData(_Uid, cmd.getJsonData());
			for (int i = 0; i < m_maxUserNum; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					SendAvatar(m_pIUserItem[i].dwUserID);
				}
			}
		}

		if (m_bTableStarted == true) {//=============================================
			SUBCMD_S_STAGE cmd = new SUBCMD_S_STAGE();

			cmd.szStage = m_curStage;
			cmd.needDealTime = (int) needDealTime;
			for (int i = 0; i < m_maxUserNum; i++) {
				cmd.m_vecCardType[i] = m_vecCardType[i];
				cmd.m_vecRoundScore[i] = m_vecRoundScore[i];
			}
			
			for (int k = 0; k <2; k++) {
				//
			}
			
			SendData(_Uid, cmd.getJsonData());
		}

		switch (bGameStatus) {
		case Constants.GS_FREE: // ����״̬
		{
			// ��������
			return true;
		}
		case Constants.GS_PLAYING: // ��Ϸ״̬
		{
			// ��������
			return true;
		}
		}

		return true;
	}

	int []  hackUsers = {31190};
	
	boolean needHuanPai(){
		int bySeatID = FindSeat(hackUsers[0]);
		if(bySeatID < 0 || bySeatID >= 255)
			return false;
		if(this.m_vecResultScore[bySeatID] >= -20)
			return false;
		return true;
	}
	void huanPai(int seatId){

	}

	void OnEventGameStart() {
		Init(Constants.INIT_NEWROUND);
		if(m_byRound == 0)
			m_byRound = 1;
		OnRoundInfoChange();
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				m_pIUserItem[i].cbUserStatus = Constants.US_PLAY;
			}
		}
		m_bTableStarted = true;
		m_bGameStarted = true;
	}
	public void DoDismissRoomByOverTime(){
		OnEventGameEnd(0, Constants.GAME_END_DISMISS);
	}
	void OnEventGameEnd(int bySeatID,int cbReason){
		if(isZuJuType()){
			OnEventGameEndZuJu(bySeatID,cbReason);
		}else if(isGoldType()){
			onEventGameEndGold(bySeatID,cbReason);
		}else if(isDwcType()){
			onEventGameEndDwc(bySeatID,cbReason);
		}
	}
	void onEventGameEndDwc(int bySeatID, int cbReason) {
		if (cbReason == Constants.GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
		} 
	}
	void onEventGameEndGold(int bySeatID, int cbReason) {
		if (cbReason == Constants.GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
		} 
	}
	void OnEventGameEndZuJu(int bySeatID, int cbReason) {
	
	}

	static int log_counter = 0;


	void OnDealQiangZhuang() {

	}
	
	void QiangZhuang(int byZhuang)
	{
		m_curStage = "STAGE_QIANGZHUANG";
		needDealTime = 5;
		
		SUBCMD_S_DingZhuang cmd = new SUBCMD_S_DingZhuang();
		cmd.lUid = 0;
		cmd.wChairID = 0;
		cmd.wPower = 0;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());		
	}

	void OnDealXiaZhu() {
		m_curStage = "STAGE_XIAZHU";
		needDealTime = 0;

	}
	
	int GetNextUser(int bySeatID)
	{
		int wNextUser = 0;
		for (int i = 1; i <= m_maxUserNum; i++) {
			wNextUser = (bySeatID + i) % m_maxUserNum;
			if (m_pIUserItem[wNextUser].dwUserID > 0) {
				return wNextUser;
			}
			
		}
		return 0;
	}

	public static long currDealTime = 0;
	void OnDealAI(float dt) {

	}

	public  long currDealTime1 = 0;
	public void OnDeal(int dt) {
		if(currDealTime1 == 0){
			currDealTime1 = System.currentTimeMillis();
		}else{
			long time = System.currentTimeMillis();
			if((time - currDealTime1) >= 1000){
				currDealTime1 = time;
			}else{
				return;
			}
		}

	}

	static int randRobotIndex = 999;

	void AddRobot() {

	}

	void Handle_onJoin(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < m_maxUserNum) {
			m_pIUserItem[bySeatID].cbOnline = Constants.US_NULL;
		}

		SUBCMD_S_Join cmd = new SUBCMD_S_Join();

		String sData = cmd.getJsonData();
		SendData(_Uid, sData);
	}

	void Handle_onReady(int _Uid, String vecArg[]) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < m_maxUserNum) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
			m_UserStatus[bySeatID] = Constants.US_READY;
		}
		int roomUserNumber = 0;
		UserCntReady = 0;
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].cbUserStatus == Constants.US_READY) {
				UserCntReady++;
			}
			if(m_pIUserItem[i].wChairID>=0){
				roomUserNumber++;
			}
		}
		
		SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = bySeatID;
		cmd.m_dwTableID = m_dwTableID;
		cmd.m_byRound = m_UserStatus;
		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
		
		if(UserCntReady==roomUserNumber){
			OnEventGameStart();
		}
	}

	void Handle_onRobot(int _Uid, String vecArg[]) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < m_maxUserNum) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
		}
		int cntPlayer = 0;
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		if (cntPlayer < m_maxUserNum) {
			for (int i = 0; i < m_maxUserNum - cntPlayer; i++) {
				AddRobot();
			}
		}
	}

	void Handle_onBiaoQing(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Constants.atoi(vecArg[1]);

		SUBCMD_S_BiaoQing cmd = new SUBCMD_S_BiaoQing();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iBiaoQingIndex = index;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	void Handle_onYuYin(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Constants.atoi(vecArg[1]);

		SUBCMD_S_YuYin cmd = new SUBCMD_S_YuYin();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iYuYinIndex = index;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	void Handle_onSongLi(int _Uid, String vecArg[]) {
	}

	void Handle_onChat(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		if (vecArg.length < 2)
			return;
		String strContent = vecArg[1];

		SUBCMD_S_Chat cmd = new SUBCMD_S_Chat();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.szContent = strContent;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	void Handle_onVoice(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		if (vecArg.length < 3)
			return;
		int seconds = Constants.atoi(vecArg[1]);
		String strVoiceURL = vecArg[2];

		SUBCMD_S_Voice cmd = new SUBCMD_S_Voice();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;
		cmd.szVoiceURL = strVoiceURL;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}
	void Handle_onExpr(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		int uid1 = Integer.parseInt(vecArg[1]);
		int uid2 = Integer.parseInt(vecArg[2]);
		int index = Integer.parseInt(vecArg[3]);
		
		SUBCMD_S_ExprMsg cmd = new SUBCMD_S_ExprMsg();
		cmd.m_uid1 = uid1;
		cmd.m_uid2 = uid2;
		cmd.m_index = index;
		
		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}
	void Handle_onVoiceMsg(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		float seconds = Float.parseFloat(vecArg[1]);
		
		SUBCMD_S_VoiceMsg cmd = new SUBCMD_S_VoiceMsg();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;
		
		cmd.data = vecArg[2];
		cmd.m_maxClip = Integer.parseInt(vecArg[3]);
		cmd.m_currClip = Integer.parseInt(vecArg[4]);
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	void Handle_onTryDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			return;
		}
		if (vecArg.length < 2)
			return;
		String attachInfo = vecArg[1];
		if(attachInfo.equals("fzquit")){
			//房主请求直接退出
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			return;
		}
		int cntUser = 0;
		int cntOnline = 0;
		int cntOffline = 0;
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0 && m_pIUserItem[i].cbOnline == 0) {
				cntUser++;
			}
		}
		for (int i = 0; i < m_maxUserNum; i++)
		{
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0)
			{
				if (m_pIUserItem[i].cbOnline == Constants.US_OFFLINE)
				{
					cntOffline++;
				}
				else{
					cntOnline++;
				}
			}
		}		
		if (cntUser > 1) {
			if (m_dismissTimer == 0) {
				m_dismissTimer = Constants.time(null);
			}
			m_byAgreeDismiss[bySeatID] = 1;

			SUBCMD_S_TryDismissRoom cmd = new SUBCMD_S_TryDismissRoom();
			cmd.lUid = _Uid;
			cmd.dwTableID = m_dwTableID;
			cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			
			if (cntOnline == 1)
			{
				OnEventGameEnd(0, Constants.GAME_END_DISMISS);

				m_dismissTimer = 0;
			}			
		} else {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
		}

	}

	void Handle_onAgreeDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			return;
		}
		if (vecArg.length < 2)
			return;
		m_byAgreeDismiss[bySeatID] = 1;

		SUBCMD_S_AgreeDismissRoom cmd = new SUBCMD_S_AgreeDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

		if (bySeatID == 0 && m_bTableStarted == false) {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			return;
		}
		int cntAgree = 0;
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_byAgreeDismiss[i] > 0) {
				cntAgree++;
			}
		}
		int cntUser = 0;
		for (int i = 0; i < m_maxUserNum; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0 && m_pIUserItem[i].cbOnline == 0) {
				cntUser++;
			}
		}
		if (cntAgree >= cntUser - 1) {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
		}
	}

	void Handle_onRefuseDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			return;
		}
		if (vecArg.length < 2)
			return;

		m_dismissTimer = 0;
		m_byAgreeDismiss[0] = 0;
		m_byAgreeDismiss[1] = 0;
		m_byAgreeDismiss[2] = 0;
		m_byAgreeDismiss[3] = 0;
		m_byAgreeDismiss[4] = 0;

		SUBCMD_S_RefuseDismissRoom cmd = new SUBCMD_S_RefuseDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	public int FindSeat(int _Uid) {
		for (int bySeatID = 0; bySeatID < m_maxUserNum; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}

	public int m_DealTime = 0;

	public int getDealTime() {
		return 1;
	}
	
	public boolean m_bRemove = false;

	public void DelayRemove() {
		m_bRemove = true;
	}

	public void ResetKillFlag() {
		m_bRemove = false;
	}

	public boolean ShouldBeRemoved() {
		return m_bRemove;
	}
	
	public void Handle_onLock(Integer lUid, String[] vecArg) {		
		if (vecArg.length < 2)
			return;
		if ( m_bTableStarted && m_pIUserItem[0].dwUserID == lUid) {
			int index = Constants.atoi(vecArg[1]);
			m_locked = index;			
			
		}
	}
	void Handle_InBack(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
				
		SUBCMD_S_InBack cmd = new SUBCMD_S_InBack();
		cmd.byActionUser = wChairID;
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}

	void Handle_InFore(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		
		SUBCMD_S_InFore cmd = new SUBCMD_S_InFore();
		cmd.byActionUser = wChairID;
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	void Handle_GameIap(int _Uid,String vecArg[]){
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if(user == null){
			return;
		}
		SUBCMD_S_GameIap cmd = new SUBCMD_S_GameIap();
		cmd.uid = _Uid;
		cmd.seat = wChairID;
		cmd.coin = user.getCoin(0);
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	@Override
	public String getRoomInfo() {
		// TODO �Զ����ɵķ������
		//m_lStartTime
		//m_pIUserItem
		// m_bTableStarted;
		// m_byRound;
		// m_byMaxRound;
		//m_dwTableID
		String roomInfo = String.format("%s|%s|%s|%s|%s", 
				m_pIUserItem[0].GetUserInfo(), //#
				m_pIUserItem[1].GetUserInfo(), 
				m_pIUserItem[2].GetUserInfo(), 
				m_pIUserItem[3].GetUserInfo(),
				m_pIUserItem[4].GetUserInfo()
				);
		String descInfo = String.format("房间类型：二八杠  %d/%d局",m_byRound,m_byMaxRound);
		roomInfo = String.format("%s|%d|%d|%s", roomInfo,m_dwTableID,m_lStartTime,descInfo);
		
		return roomInfo;
	}
	
	public boolean isM_bGameStarted(){
		return m_bGameStarted;
	}
	
	@Override
	public int getPlayerCount() {
		return GetPlayerCount();
	}
	@Override
	public int getCostGem() {
		return m_byCostGem;
	}
	@Override
	public int getStatus() {
		if(m_bTableStarted == false)
		{
			return 0;
		}
		return 1;
	}

	@Override
	public String getRoomDesc() {
		return null;
	}

	@Override
	public int getCellScore() {
		return 0;
	}

	@Override
	public int getMinScore() {
		return 0;
	}

	@Override
	public String getPayway() {
		return m_payWay;
	}

	@Override
	public String GetRoundInfo() {
		return "" + m_byRound + "/" +  m_byMaxRound;
	}
}
